<template>
  <div class="box">
    <header class="header">
      <van-nav-bar
        title="确认订单"
        left-arrow
        @click-left="$router.back()"
      />
    </header>
    <div class="content">
      <van-cell-group>
        <van-cell @click="toAdrsslist" v-if="flag" :title="name + ' ' + tel" size="large" is-link :label="showAddress" />
        <van-cell @click="toAdrsslist" v-else title="请添加收获人信息"  size="large" is-link />
      </van-cell-group>
      <van-card
          v-for="item of cartList"
          :key="item.cartid"
          :price="item.originprice"
          :title="item.proname"
          :thumb="item.img1"
          :num="item.num"
        />
    </div>
  </div>
</template>
<script>
import Vue from 'vue'
import { NavBar, Card, Cell, CellGroup } from 'vant'
import { getCartList } from './../../api/cart'
import { confirmOrder } from '@/api/order'
Vue.use(NavBar)
Vue.use(Card)
Vue.use(Cell)
Vue.use(CellGroup)
export default {
  data () {
    return {
      orderlist: [],
      flag: true, // 判断有没有订单的地址信息
      empty: true,
      cartList: [],
      name: '',
      tel: '',
      province: '',
      city: '',
      county: '',
      addressDetail: ''
    }
  },
  computed: {
    showAddress () {
      let str = ''
      this.province === this.city
        ? str = this.province + this.county + this.addressDetail
        : str = this.province + this.city + this.county + this.addressDetail
      return str
    }
  },
  methods: {
    toAdrsslist () {
      this.$router.push('/orderaddresslist/' + this.$route.params.time)
    }
  },
  mounted () {
    confirmOrder({
      userid: localStorage.getItem('userid'),
      time: this.$route.params.time // 动态获取参数
    }).then(res => {
      console.log('000', res.data)
      this.orderlist = res.data.data
      this.flag = res.data.flag
      // ********************************
      // 收货人信息
      this.name = res.data.address.name
      this.tel = res.data.address.tel
      this.province = res.data.address.province
      this.city = res.data.address.city
      this.county = res.data.address.county
      this.addressDetail = res.data.address.addressDetail
    })
    if (localStorage.getItem('isLogin') === 'true') {
      getCartList({
        userid: localStorage.getItem('userid')
      }).then(res => {
        console.log(res.data)
        this.flag = false
        this.cartList = res.data.data
      })
    }
  }
}
</script>
